﻿﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;

namespace simDC
{
    class MicrocodeCodif
    {
        FileStream fs;
        BinaryWriter bw;
        public MicrocodeCodif()
        {
            String path = "microcodBinar.bin";
            fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
            bw = new BinaryWriter(fs);
            Codification("dana2.csv");
            CloseFile();
                    
        }
        public void CloseFile()
        {
            bw.Close();

            fs.Close();

        }

        public static Dictionary<string, char> SbusCode = new Dictionary<string, char>() 
        { 
                {"none",    (char)0x00},
                {"pdflags", (char)0x01},
                {"pdregs",  (char)0x02},
                {"pdsps",   (char)0x03},
                {"pdpcs",   (char)0x04},
                {"pdts",    (char)0x05},
                {"ptivrs",  (char)0x06},
                {"pdadrs",  (char)0x07},
                {"pdmdrs",  (char)0x08},
                {"pdirs",   (char)0x09},
                {"pdir[offset]",   (char)0x09},
                {"pd_0",   (char)0x09},
                {"pd_1s",  (char)0x09},
                {"pd_-1s", (char)0x09},
                {"pdn_ts", (char)0x09}
        };
        public static Dictionary<string, char> DbusCode = new Dictionary<string, char>()
        { 
                {"none",    (char)0x00},
                {"pdflagd", (char)0x01},
                {"pdregd",  (char)0x02},
                {"pdspd",   (char)0x03},
                {"pdpcd",   (char)0x04},
                {"pdtd",    (char)0x05},
                {"ptivrd",  (char)0x06},
                {"pdadrd",  (char)0x07},
                {"pdmdrd",  (char)0x08},
                {"pdird",   (char)0x09},
        };
        public static Dictionary<string, char> RbusCode = new Dictionary<string, char>()
        {
                {"pmflag", (char)0x00},
                {"pmreg",  (char)0x01},
                {"pmsp",   (char)0x02},
                {"pmpc",   (char)0x03},
                {"pmt",    (char)0x04},
                {"pmivr",  (char)0x05},
                {"pmadr",  (char)0x06},
                {"pmmdr",  (char)0x07}
        };
        public static Dictionary<string, ushort> AluCode = new Dictionary<string, ushort>() 
        { 
                {"none",  (char)0x00},
                {"sbus",  (char)0x01},
                {"~sbus", (char)0x02},
                {"dbus",  (char)0x03},
                {"sum",   (char)0x04},
                {"and",   (char)0x05},
                {"or",    (char)0x06},
                {"xor",   (char)0x07},
               
        };
        public static Dictionary<string, ushort> OtherCode = new Dictionary<string, ushort>();
        public static Dictionary<string, ushort> MemCode = new Dictionary<string, ushort>() 
        { 
                {"none",   (char)0x00},
                {"ifch",   (char)0x01},
                {"read",   (char)0x02},
                {"write",  (char)0x03},
        };

        public int Codification(string fileName) 
        {
            try
            {
                System.IO.StreamReader myFile = new System.IO.StreamReader(fileName);
                string line;
                char[] separator = { ',' };

                while ((line = myFile.ReadLine()) != null)
                {
                    if (line != "")
                    {
                        //sbus, dbus, alu, rbus,otherOp,memory,succesor,uadr,sel index,~t/f
                        string[] values = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < values.Length-1; i++)
                        {
                            byte x = Convert.ToByte(values[i]);
                           // byte y = Convert.ToByte(values[i+1]);
                            //hm... scriu pt fiecare camp un octet
                            bw.Write(x);
                        }
                    }
                }

                
                myFile.Close();
            }

            catch (Exception e)
            {
               
                return -1;
            }

            return 1;
        }


    }
    
}
